<?php

class Sfs_View_Helper_TopMenu
{
    private $_resource ;
    private $_baseUrl;
    private $_acl ;
    private $_username;
    public function TopMenu()
    {
        $user = Zend_Auth::getInstance()->getIdentity();
        if (!$user)
        $this->_username = 'Guest';
        else
        $this->_username = $user->username;

        $this->_acl = Zend_Registry::get('acl');
        $cache = Zend_Registry::get('cache');

        if (!$this->_resource = $cache->load('resourceAccess_cache')) {
            $aclResource = new Default_Model_AclResourcesView();
            $this->_resource = $aclResource->getAllResource();
            $cache->save($this->_resource,'resourceAccess_cache');
        }
        $this->_baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
        return    $this->_recursive( $this->_resource);

    }


    private function _checkChildAcl($res)
    {
        $flag = false;
        foreach ($res as $row) {
            if ($this->_acl->isAllowed($this->_username, $row['resource_id']))
            return true;
            // 檢查 controller 的權限
            if (isset($row['menu'])) {
                foreach ($row['menu'] as $val) {
                    $resourceName = $row['resource_id'].':'.$val['url'];
                    if ($this->_acl->has($resourceName) and $this->_acl->isAllowed($this->_username,$resourceName))                   {
                        $flag = true;
                        break;// return true;
                    }

                }
            }
            if (count($row['children'])>0)
            $flag =   $this->_checkChildAcl($row['children']);
        }
        return $flag;//false;
    }


    private function _recursive($res)
    {
        $str = "<ul>";
        foreach ($res as $row) {
            if ($row['kind'] == 'menu' and !$this->_checkChildAcl($row['children']))
            continue;

            if ($row['kind']=='module') {
                if (!$flag = $this->_acl->isAllowed($this->_username, $row['resource_id'])) {
                    // 檢查 controller 的權限
                    if ($row['menu']) {
                        foreach ($row['menu'] as $val) {
                            $resourceName = $row['resource_id'].':'.$val['url'];
                            if ($this->_acl->has($resourceName) and $this->_acl->isAllowed($this->_username,$resourceName)) {
                                $flag = true;
                                break;
                            }

                        }
                    }
                }
                if (!$flag)
                continue;
            }
            $str.="<li>";
            if ($row['kind'] == 'module')
            $str .= '<a href="'.$this->_baseUrl.'/'.$row['resource_id'].'">'. $row['title'].'</a>';
            else
            $str .= '<a href="#">'.$row['title'].'</a>';
            if ($row['children_count'] > 0)
            $str .= $this->_recursive($row['children']);
            $str .= "</li>\n" ;
        }
        $str .= "</ul>\n";
        return $str;
    }
}